package com.lianzai.wxapp.service.impl;

import com.lianzai.common.utils.StringUtils;
import com.lianzai.mobile.dao.WxUserDao;
import com.lianzai.mobile.domain.WxUserDO;
import com.lianzai.system.domain.UserDO;
import com.lianzai.user.dao.BusinessLicenseDao;
import com.lianzai.user.dao.UserInfoDao;
import com.lianzai.user.domain.BusinessLicenseDO;
import com.lianzai.user.domain.UserInfoDO;
import com.lianzai.wxapp.domain.MerchantRegisterInfo;
import com.lianzai.wxapp.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Slf4j
@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private WxUserDao wxUserDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private BusinessLicenseDao licenseDao;

    @Override
    @Transactional
    public int saveMerchant(MerchantRegisterInfo registerInfo) {

        //新增业务用户信息
        UserInfoDO user = new UserInfoDO();
        user.setMobile(registerInfo.getMobilePhone());
        user.setAddr(registerInfo.getAddress());
        user.setCommunityId(registerInfo.getCommunityId());
        user.setIndustryId(registerInfo.getIndustryId());
        user.setName(registerInfo.getName());
        user.setRole(3);
        int ret = userInfoDao.save(user);

        if (ret == 0)
        {
            return 0;
        }

        //新增微信用户信息
        WxUserDO wxUser = new WxUserDO();
        wxUser.setOpenId(registerInfo.getOpenId());
        wxUser.setUserId(user.getId());

        try {
            ret = wxUserDao.save(wxUser);
            if (ret == 0) {
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();

            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

        //保存营业执照照片路径
        if (StringUtils.isNotBlank(registerInfo.getBusinessLicensePhotoPath()))
        {
            BusinessLicenseDO license = new BusinessLicenseDO();
            license.setUserId(user.getId());
            license.setLicensePhotoPath(registerInfo.getBusinessLicensePhotoPath());

            try {
                ret = licenseDao.save(license);
                if (ret == 0) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return 0;
                }
            } catch (Exception e) {
                e.printStackTrace();

                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return 0;
            }
        }

        return ret;
    }

    @Override
    @Transactional
    public int updateMerchant(MerchantRegisterInfo registerInfo) {

        //新增业务用户信息
        UserInfoDO user = new UserInfoDO();
        user.setId(registerInfo.getUserId());
        user.setMobile(registerInfo.getMobilePhone());
        user.setAddr(registerInfo.getAddress());
        user.setIndustryId(registerInfo.getIndustryId());
        user.setName(registerInfo.getName());
        user.setAuditStatus(0);
        int ret = userInfoDao.update(user);

        if (ret == 0)
        {
            return 0;
        }

        //保存营业执照照片路径
        if (StringUtils.isNotBlank(registerInfo.getBusinessLicensePhotoPath()))
        {
            try {
                BusinessLicenseDO license = licenseDao.getByUser(user.getId());
                if (license != null) {
                    license.setLicensePhotoPath(registerInfo.getBusinessLicensePhotoPath());
                    ret = licenseDao.update(license);
                    if (ret == 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

                        return 0;
                    }
                } else {
                    //license不存在，则新增license
                    license = new BusinessLicenseDO();
                    license.setUserId(user.getId());
                    license.setLicensePhotoPath(registerInfo.getBusinessLicensePhotoPath());
                    ret = licenseDao.save(license);
                    if (ret == 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return 0;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return 0;
            }
        }

        return ret;
    }

    @Override
    public String getLicensePath(Long userId)
    {

        BusinessLicenseDO licenseDO = null;
        try {
            licenseDO = licenseDao.getByUser(userId);
        } catch (Exception e) {
            log.error("get business license path error.", e);
            return null;
        }

        if (licenseDO != null)
        {
            return licenseDO.getLicensePhotoPath();
        }
        else
        {
            return null;
        }

    }
}
